﻿@using KPIS.GERP.Models.ICM
@using KPIS.GERP.Models.ICM.ViewModel
@model vmPrVsPo

@{
    // Layout = "~/Views/Shared/_EmptyLayout.cshtml";

    ViewBag.Title = "งานใบสั่งหรือสัญญา [PO]";
    ViewBag.Descr = ViewBag.Title + " เพื่อทำใบสั่งหรือสัญญาซื้อ/จ้างโดยหน่วยงานพัสดุ";
}

@using (Html.BeginForm("Create", "PurchaseOrder", FormMethod.Post, new { @class = "form-horizontal" }))
{
    @Html.AntiForgeryToken()

    <div id="loading">
        <div id="loadingcontent">
            <p id="loadingspinner">
                Loading...
            </p>
        </div>
    </div>
    <p id="message"></p>
    <div class="block-button-menu-above">
        @Html.ButtonViewAll()
        @Html.ButtonSubmit()
    </div>
    
   <div class="alert-error">@Html.ValidationSummary()</div>
    @Html.Partial("_EditorForm")
     
}




@section scripts
{
      <style type="text/css">
    #loading
    {
        display:none;
        position:fixed;
        left:0;
        top:0;
        width:100%;
        height:100%;
        background:rgba(255,255,255,0.8);
        z-index:1000;
    }
  
    #loadingcontent
    {
        display:table;
        position:fixed;
        left:0;
        top:0;
        width:100%;
        height:100%;
    }
  
    #loadingspinner
    {
        display: table-cell;
        vertical-align:middle;
        width: 100%;
        text-align: center;
        font-size:larger;
        padding-top:80px;
    }

    
</style>

      <script src="~/Content/Ace/assets/js/spin.min.js"></script>
      <script>
        

          $("form").not("#formsearch").submit(function (e) {
            e.preventDefault();
            var currentForm = this;
            bootbox.confirm("ท่านต้องการยืนยัน ใช่หรือไม่", function (result) {
                if (result) {
                    // currentForm.submit();
                    $.ajax({
                        //beforeSend: function () {
                        //    $("#loading").fadeIn();
                        //    var opts = {
                        //        lines: 12, // The number of lines to draw
                        //        length: 7, // The length of each line
                        //        width: 4, // The line thickness
                        //        radius: 10, // The radius of the inner circle
                        //        color: '#000', // #rgb or #rrggbb
                        //        speed: 1, // Rounds per second
                        //        trail: 60, // Afterglow percentage
                        //        shadow: false, // Whether to render a shadow
                        //        hwaccel: false // Whether to use hardware acceleration
                        //    };
                        //    var target = document.getElementById('loading');
                        //    var spinner = new Spinner(opts).spin(target);
                        //   // $(target).data('spinner', spinner);
                        //},
                        url: "Create",
                        type: "post",
                        dataType: "json",
                        data: $(currentForm).serialize(),
                        success: function (result) {
                           // console.log(result.success);
                           // console.log(result.confirm);
                            if (result.success) {
                               // $("#loading").hide();
                               
                                window.setTimeout(function () {
                                    $("#message").html(result.message);
                                    window.location.href = "@Url.Action("Details", "PurchaseOrder")?id=" + result.PoId;
                                }, 1000);
                            }
                            else {
                                $("#message").html(result.message);
                               //  $("#loading").hide();
                            }
                        }
                    });
                }
            });
        });

      
        $("#PurchaseOrder_RedemptionPercent").keyup(function () {
            var text = parseFloat($(this).val());
            //alert(text);
            //!isNaN('-1')
           // if (text.toString().length >= 1) {
                if (isNaN(text)) {
                    alert("กรุณากรอกข้อมูลตัวเลข!");
                    $("#PurchaseOrder_RedemptionPercent").val('0');
                }
                if (text > 100) {
                    alert("กรุณากรอกข้อมูล 1-100!");
                }
            //}
          //  alert(text);
            });
        
       
        //function GetCellValues(index) {
        //    var table = document.getElementById('table');
        //   // for (var r = 0, n = table.rows.length; r < n; r++) {
        //    //  for (var c = 0, m = table.rows[r].cells.length; c < m; c++) {
            
        //    var value = table.rows[index].cells[6].innerHTML;
        //    alert(value);
        //    if (value == 'Y') {

        //        $('#contract').fadeIn();
        //        return;
        //    } else {
        //        $('#contract').slideToggle();
        //        return;
        //    }
        //}

        //$("tr").click(function () {
        //    var index = $("tr").index(this);
        //    GetCellValues(index);
        //});

  
     
        $(function () {
            ChangeEnableDurType();
        });

        $('input[type=radio]').change(ChangeEnableDurType);

        function ChangeEnableDurType() {
           


            if ($('#durType2radio').is(':checked')) {
                $('#PurchaseOrder_RequireDuration').attr('disabled', '');
                $('#PurchaseOrder_DurTypeId').attr('disabled', '');
               // $('#DurCountId').attr('disabled', '');
                $('#PurchaseOrder_RequireDate').removeAttr('disabled');
            } else {
                $('#PurchaseOrder_RequireDuration').removeAttr('disabled');
                $('#PurchaseOrder_DurTypeId').removeAttr('disabled');
             //   $('#DurCountId').removeAttr('disabled');
                $('#PurchaseOrder_RequireDate').attr('disabled', '');
            }
        }
    </script>
}
